import importlib

from app.utils.rewrite import CustomRequestParser


def get_parser(parser_name: str, arg_group: str):
    """
    Relect params
    Example: args = parse_args('index', 'index_rules')
    location: query|header|formData|body|cookie
    """
    _parser = importlib.import_module('app.parser.{}'.format(parser_name))
    _args = getattr(_parser, arg_group)
    parser = CustomRequestParser()
    for _arg in _args:
        _location = _arg.get('location')
        _arg['rule']['location'] = _location if _location else 'query'  # default query
        parser.add_argument(_arg['field'], **_arg['rule'])
    return parser
    